<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Page
 *
 * @author Adriano_2
 */
abstract class Page {
    /**
     * Define if show or not HTML Header
     * @var boolean
     */
    protected $showHTML = true;
    
    /**
     * Define if show or not Page Header
     * @var boolean
     */
    protected $showPage = true;
    
    /**
     * The page title
     * @var string 
     */
    protected $title = "DREDS";
    
    /**
     * Default action method, that will be called if nothing is passed
     * @var string 
     */
    protected $action;
    
    /**
     * Add extra javascript code
     */
    protected function showExtraJavascript() {
        
    }
    
    /**
     * Add extra CSS code
     */
    protected function showExtraCSS() {
        
    }
    
    /**
     * Show HTML Header
     * @param string $titulo
     */
    protected function showHTMLHeader() {
        ?>
        <!DOCTYPE html>
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title><?php echo $this->title; ?></title>
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                
                <!-- Bootstrap -->
                <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
                <link href="css/estilo.css" rel="stylesheet" media="screen">
                <?php $this->showExtraCSS(); ?>
            </head>
            <body>
        <?php
    }
    
    /**
     * Show page header
     */
    protected function showPageHeader() {
        ?>
       <div class="container">

            <div class="navbar navbar-fixed-top">
              <div class="navbar-inner">
                <div class="container">
                  <a class="brand" href="<?php echo BASE_URL; ?>">Drediada</a>
                  <div class="btn-group pull-right">
                    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                      <i class="icon-user"></i> Usuário
                      <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Meus dreds</a></li>
                      <li class="divider"></li>
                      <li><a href="logout">Logout</a></li>
                    </ul>
                  </div>
                  <div class="nav-collapse">
                    <ul class="nav">
                    <?php
                        $menu = array(
                          'main' => 'Home',
                          'dreds' => 'Dreds',
                          'editor' => 'Novo Dred'
                        );
                        foreach ($menu as $link => $label) {
                            $class = '';
                            $part = explode('/', $link);
                            if ($part[0] == $_GET['_page']) {
                                if (count($part) == 1) {
                                    $class = 'class="active"';
                                }
                                else if (count($part) == 2 && $part[1] == $_GET['_action']) {
                                    $class = 'class="active"';
                                }
                            }
                            echo "<li {$class}><a href='".BASE_URL."{$link}'>{$label}</a></li>";
                        }
                    ?>
                    </ul>
                    
                  </div><!--/.nav-collapse -->
                </div>
              </div>
           </div>
           <div class="row">
        <?php
    }
    
    /**
     * Show page footer
     */
    protected function showPageFooter() {
        ?>
           <hr>
           </div>
           
      <footer>
        <p>© Todos os direitos reservados | 2012</p>
      </footer>
    </div>
        <?php
    }
    
    /**
     * Show HTML Footer
     */
    protected function showHTMLFooter() {
        ?>
                <script src="js/jquery-1.9.1.min.js"></script>
                <script src="js/bootstrap.min.js"></script>
                <?php $this->showExtraJavascript(); ?>
            </body>
        </html>
        <?php
    }
    
    /**
     * Render page
     */
    final public function render() {
        if ($this->showHTML) {
            $this->showHTMLHeader();
            if ($this->showPage) {
                $this->showPageHeader();
            }
        }
        
        $this->renderPage();
        
        if ($this->showHTML) {
            if ($this->showPage) {
                $this->showPageFooter();
            }
            $this->showHTMLFooter();
        }
    }
    
    /**
     * Method to load dincamic page
     * @param string $className The class name
     * @param string $methodName The method name
     * 
     * @return Page
     */
    public static function load($className, $methodName) {
       if(!class_exists($className)) return false;
       $page = new $className;
       if (!empty($methodName)) {
           $page->action = $methodName;
       }
       return $page;
    }
   
    /**
     * Method executed to call method dinamically or overwrite by developer
     */
    protected function renderPage() {
        if (!empty($this->action)) {
            $method = $_SERVER['REQUEST_METHOD'] . ucfirst($this->action);
            if (method_exists($this, $method)) {
                $this->$method();
            }
            else {
                die('<p>Implement method <b>'.$method.'</b><p>');
            }
        }
    }
    
}
 